package com.leyou.item.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.entiey.Sku;
import com.leyou.item.entiey.SpecParam;
import com.leyou.item.entiey.Spu;
import com.leyou.item.entiey.SpuDetail;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpecParamService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private SpuService spuService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private SpuDetailService spuDetailService;

    /**
     * 分页查询
     * @param page     当前页
     * @param rows     每页大小
     * @param saleable 上架商品或下降商品
     * @param brandId 品牌id
     * @param categoryId 分类id
     * @param id  spu的id
     * @return 当前页商品数据
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage(
            @RequestParam(value = "brandId",required = false) Long brandId,
            @RequestParam(value = "categoryId",required = false) Long categoryId,
            @RequestParam(value = "id",required = false) Long id,
            @RequestParam(value = "page",defaultValue = "1") Long page,
            @RequestParam(value = "rows",defaultValue = "5") Long rows,
            @RequestParam(value = "saleable",required = false) Boolean saleable){
        //调用分页查询
        PageDTO<SpuDTO> pageDTO =  spuService.querySpuByPage(brandId,categoryId,id,page,rows,saleable);

        return ResponseEntity.ok(pageDTO);

    }

    /**
     * 添加商品
     * @param spuDTO  页面请求参数
     * @return void
     */
    @PostMapping
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){
        //调用添加方法
        spuService.saveGoods(spuDTO);

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 商品上下架
     * @param id 商品id
     * @param saleable 是否上架
     * @return void
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSaleableById(@RequestParam("id") Long id,
                                               @RequestParam("saleable") Boolean saleable){

        spuService.updateSaleableById(id,saleable);

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据id查询商品信息
     * @param id 商品id
     * @return 商品信息
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") String id){
/*        //根据id查询
        Spu spu = spuService.getById(id);
        //转换为dto对象
        SpuDTO spuDTO = new SpuDTO(spu);
        //返回结果
        return ResponseEntity.ok(spuDTO);*/
        return ResponseEntity.ok(new SpuDTO(spuService.getById(id)));
    }

    /**
     * 根据id查询商品详情
     * @param id 商品id
     * @return 返回商品详情（tb_spuDetail）
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id") Long id){
        return ResponseEntity.ok(new SpuDetailDTO(spuDetailService.getById(id)));
    }


    /**
     * 根据sku的id集合 查询sku信息
     * @param ids id集合
     * @return sku信息的集合
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuDTOListByIds(@RequestParam("ids") List<Long> ids){
        //根据ids查修你sku信息的集合
        List<Sku> skus = skuService.listByIds(ids);
        //返回结果
        return ResponseEntity.ok(SkuDTO.convertEntityList(skus));
    }


    /**
     * 根据spuID查询sku的集合
     * @param id spuID
     * @return sku集合
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuDTOListById(@RequestParam("id") Long id){
        return ResponseEntity.ok(SkuDTO.convertEntityList(skuService.querySkuDTOListById(id)));
    }


    /**
     *  根据spuId查询spu的所有规格参数值
     * @param id spuIO
     * @param searching 是否参与查询
     * @return 规格参数集合
     */

    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecParamById(@RequestParam("id") Long id,
                                   @RequestParam(value = "searching",required = false) Boolean searching){

        List<SpecParamDTO> specParamDTOList = spuDetailService.querySpecParamById(id,searching);

        return ResponseEntity.ok(specParamDTOList);

    }

    /**
     * 根据id查询spu及sku、spuDetail等
     * @param id 商品id
     * @return  返回商品集合
     */
    @GetMapping("{id}")
    public ResponseEntity<SpuDTO> querySpuAndSkuAndSpuDetailById(@PathVariable("id") Long id){

        SpuDTO spuDTO = spuService.querySpuAndSkuAndSpuDetailById(id);

        return ResponseEntity.ok(spuDTO);
    }


    /**
     * 更新商品
     * @param spuDTO  商品信息
     * @return void
     */
    @PutMapping
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){

        spuService.updateGoods(spuDTO);

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }


    /**
     * 扣减库存
     * @param cartMap 商品集合
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> deductStock(@RequestBody Map<Long, Integer> cartMap){
        skuService.deductStock(cartMap);
        return ResponseEntity.noContent().build();
    }


}
